/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */



package com.android1.amarena2d.actions.movement;

import com.android1.amarena2d.actions.base.IntervalAction;
import com.android1.amarena2d.nodes.behavior.ActionTarget;

public class MoveTo extends IntervalAction {

    protected float endPositionX;
    protected float endPositionY;
    protected float startPositionX;
    protected float startPositionY;
    protected float deltaX;
    protected float deltaY;

    public static MoveTo $(float x, float y, float duration) {
        return new MoveTo(x, y, duration);
    }

    public MoveTo reset(float x, float y, float duration) {
        this.duration = duration;
        this.endPositionX = x;
        this.endPositionY = y;
        return this;
    }

    public MoveTo(float x, float y, float duration) {
        super(duration);
        endPositionX = x;
        endPositionY = y;
    }

    @Override
    public void start(ActionTarget target) {
        super.start(target);
        startPositionX = target.getX();
        startPositionY = target.getY();
        deltaX = endPositionX - startPositionX;
        deltaY = endPositionY - startPositionY;
    }


    @Override
    public void update(float time) {
        target.setXY(startPositionX + deltaX * time, startPositionY + deltaY * time);
    }

    @Override
    public IntervalAction reverse() {
        return MoveTo.$(startPositionX, startPositionY, duration);
    }

    @Override
    public MoveTo copy() {
        return MoveTo.$(endPositionX, endPositionY, duration);
    }

}
